<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>BadgerDB: bufmgr/src/page.cpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">BadgerDB
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">bufmgr/src/page.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00009"></a>00009 
<a name="l00010"></a>00010 <span class="preprocessor">#include &quot;exceptions/insufficient_space_exception.h&quot;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &quot;exceptions/invalid_record_exception.h&quot;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &quot;exceptions/invalid_slot_exception.h&quot;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &quot;exceptions/slot_in_use_exception.h&quot;</span>
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;page_iterator.h&quot;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;page.h&quot;</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="keyword">namespace </span>badgerdb {
<a name="l00018"></a>00018 
<a name="l00019"></a><a class="code" href="classbadgerdb_1_1_page.html#a1e4a3930a92ed6e71ffd253cf224379a">00019</a> <a class="code" href="classbadgerdb_1_1_page.html#a1e4a3930a92ed6e71ffd253cf224379a">Page::Page</a>() {
<a name="l00020"></a>00020   initialize();
<a name="l00021"></a>00021 }
<a name="l00022"></a>00022 
<a name="l00023"></a>00023 <span class="keywordtype">void</span> Page::initialize() {
<a name="l00024"></a>00024   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#ac71c5a9b3b8644da0ea827f5bd4c5742">free_space_lower_bound</a> = 0;
<a name="l00025"></a>00025   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a3af3c066d7651294fb3aae104f404648">free_space_upper_bound</a> = <a class="code" href="classbadgerdb_1_1_page.html#a74057ec71412352ef0aa5913bbebed25">DATA_SIZE</a>;
<a name="l00026"></a>00026   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a> = 0;
<a name="l00027"></a>00027   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a> = 0;
<a name="l00028"></a>00028   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a3f721c5ce9ef491fdc9d12292f2f4498">current_page_number</a> = <a class="code" href="classbadgerdb_1_1_page.html#a785a1e756d47fb7f8f3603a3fe8ffcef">INVALID_NUMBER</a>;
<a name="l00029"></a>00029   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7cb8260dedb0748d3d75c4b1722dcf3c">next_page_number</a> = <a class="code" href="classbadgerdb_1_1_page.html#a785a1e756d47fb7f8f3603a3fe8ffcef">INVALID_NUMBER</a>;
<a name="l00030"></a>00030   data_.assign(<a class="code" href="classbadgerdb_1_1_page.html#a74057ec71412352ef0aa5913bbebed25">DATA_SIZE</a>, <span class="keywordtype">char</span>());
<a name="l00031"></a>00031 }
<a name="l00032"></a>00032 
<a name="l00033"></a><a class="code" href="classbadgerdb_1_1_page.html#ad0b1e85fe7849fb767dd8c21a8053cf4">00033</a> <a class="code" href="structbadgerdb_1_1_record_id.html" title="Identifier for a record in a page.">RecordId</a> <a class="code" href="classbadgerdb_1_1_page.html#ad0b1e85fe7849fb767dd8c21a8053cf4">Page::insertRecord</a>(<span class="keyword">const</span> std::string&amp; record_data) {
<a name="l00034"></a>00034   <span class="keywordflow">if</span> (!<a class="code" href="classbadgerdb_1_1_page.html#a35bf91df5caa67a606681519740848b0">hasSpaceForRecord</a>(record_data)) {
<a name="l00035"></a>00035     <span class="keywordflow">throw</span> <a class="code" href="classbadgerdb_1_1_insufficient_space_exception.html" title="An exception that is thrown when a record is attempted to be inserted into a page that doesn&#39;t have s...">InsufficientSpaceException</a>(
<a name="l00036"></a>00036         <a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>(), record_data.length(), <a class="code" href="classbadgerdb_1_1_page.html#af3d324b5aae656030465e213e86e2070">getFreeSpace</a>());
<a name="l00037"></a>00037   }
<a name="l00038"></a>00038   <span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> slot_number = getAvailableSlot();
<a name="l00039"></a>00039   insertRecordInSlot(slot_number, record_data);
<a name="l00040"></a>00040   <span class="keywordflow">return</span> {<a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>(), slot_number};
<a name="l00041"></a>00041 }
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="classbadgerdb_1_1_page.html#a77b352920c3e66384317bbcdcd900d99">00043</a> std::string <a class="code" href="classbadgerdb_1_1_page.html#a77b352920c3e66384317bbcdcd900d99">Page::getRecord</a>(<span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_record_id.html" title="Identifier for a record in a page.">RecordId</a>&amp; record_id)<span class="keyword"> const </span>{
<a name="l00044"></a>00044   validateRecordId(record_id);
<a name="l00045"></a>00045   <span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>&amp; slot = getSlot(record_id.<a class="code" href="structbadgerdb_1_1_record_id.html#a19ec7b749099499446fdd24d48cec9ef">slot_number</a>);
<a name="l00046"></a>00046   <span class="keywordflow">return</span> data_.substr(slot.<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a>, slot.<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>);
<a name="l00047"></a>00047 }
<a name="l00048"></a>00048 
<a name="l00049"></a><a class="code" href="classbadgerdb_1_1_page.html#ae24e3f6076d6c0a18d4dd2014d540bab">00049</a> <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_page.html#ae24e3f6076d6c0a18d4dd2014d540bab">Page::updateRecord</a>(<span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_record_id.html" title="Identifier for a record in a page.">RecordId</a>&amp; record_id,
<a name="l00050"></a>00050                         <span class="keyword">const</span> std::string&amp; record_data) {
<a name="l00051"></a>00051   validateRecordId(record_id);
<a name="l00052"></a>00052   <span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>* slot = getSlot(record_id.<a class="code" href="structbadgerdb_1_1_record_id.html#a19ec7b749099499446fdd24d48cec9ef">slot_number</a>);
<a name="l00053"></a>00053   <span class="keyword">const</span> std::size_t free_space_after_delete =
<a name="l00054"></a>00054       <a class="code" href="classbadgerdb_1_1_page.html#af3d324b5aae656030465e213e86e2070">getFreeSpace</a>() + slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>;
<a name="l00055"></a>00055   <span class="keywordflow">if</span> (record_data.length() &gt; free_space_after_delete) {
<a name="l00056"></a>00056     <span class="keywordflow">throw</span> <a class="code" href="classbadgerdb_1_1_insufficient_space_exception.html" title="An exception that is thrown when a record is attempted to be inserted into a page that doesn&#39;t have s...">InsufficientSpaceException</a>(
<a name="l00057"></a>00057         <a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>(), record_data.length(), free_space_after_delete);
<a name="l00058"></a>00058   }
<a name="l00059"></a>00059   <span class="comment">// We have to disallow slot compaction here because we&#39;re going to place the</span>
<a name="l00060"></a>00060   <span class="comment">// record data in the same slot, and compaction might delete the slot if we</span>
<a name="l00061"></a>00061   <span class="comment">// permit it.</span>
<a name="l00062"></a>00062   <a class="code" href="classbadgerdb_1_1_page.html#a1bc17754b4ed548f8520a6274b3a4202">deleteRecord</a>(record_id, <span class="keyword">false</span> <span class="comment">/* allow_slot_compaction */</span>);
<a name="l00063"></a>00063   insertRecordInSlot(record_id.<a class="code" href="structbadgerdb_1_1_record_id.html#a19ec7b749099499446fdd24d48cec9ef">slot_number</a>, record_data);
<a name="l00064"></a>00064 }
<a name="l00065"></a>00065 
<a name="l00066"></a><a class="code" href="classbadgerdb_1_1_page.html#a1bc17754b4ed548f8520a6274b3a4202">00066</a> <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_page.html#a1bc17754b4ed548f8520a6274b3a4202">Page::deleteRecord</a>(<span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_record_id.html" title="Identifier for a record in a page.">RecordId</a>&amp; record_id) {
<a name="l00067"></a>00067   <a class="code" href="classbadgerdb_1_1_page.html#a1bc17754b4ed548f8520a6274b3a4202">deleteRecord</a>(record_id, <span class="keyword">true</span> <span class="comment">/* allow_slot_compaction */</span>);
<a name="l00068"></a>00068 }
<a name="l00069"></a>00069 
<a name="l00070"></a>00070 <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_page.html#a1bc17754b4ed548f8520a6274b3a4202">Page::deleteRecord</a>(<span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_record_id.html" title="Identifier for a record in a page.">RecordId</a>&amp; record_id,
<a name="l00071"></a>00071                         <span class="keyword">const</span> <span class="keywordtype">bool</span> allow_slot_compaction) {
<a name="l00072"></a>00072   validateRecordId(record_id);
<a name="l00073"></a>00073   <a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>* slot = getSlot(record_id.<a class="code" href="structbadgerdb_1_1_record_id.html#a19ec7b749099499446fdd24d48cec9ef">slot_number</a>);
<a name="l00074"></a>00074   data_.replace(slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a>, slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>, slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>, <span class="charliteral">&#39;\0&#39;</span>);
<a name="l00075"></a>00075 
<a name="l00076"></a>00076   <span class="comment">// Compact the data by removing the hole left by this record (if necessary).</span>
<a name="l00077"></a>00077   std::uint16_t move_offset = slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a>; 
<a name="l00078"></a>00078   std::size_t move_bytes = 0;
<a name="l00079"></a>00079   <span class="keywordflow">for</span> (<a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> i = 1; i &lt;= header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a>; ++i) {
<a name="l00080"></a>00080     <a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>* other_slot = getSlot(i);
<a name="l00081"></a>00081     <span class="keywordflow">if</span> (other_slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a4ea5ad6e73525244bb368181f21fd018">used</a> &amp;&amp; other_slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a> &lt; slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a>) {
<a name="l00082"></a>00082       <span class="keywordflow">if</span> (other_slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a> &lt; move_offset) {
<a name="l00083"></a>00083         move_offset = other_slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a>;
<a name="l00084"></a>00084       }
<a name="l00085"></a>00085       move_bytes += other_slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>;
<a name="l00086"></a>00086       <span class="comment">// Update the slot for the other data to reflect the soon-to-be-new</span>
<a name="l00087"></a>00087       <span class="comment">// location.</span>
<a name="l00088"></a>00088       other_slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a> += slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>;
<a name="l00089"></a>00089     }
<a name="l00090"></a>00090   }
<a name="l00091"></a>00091   <span class="comment">// If we have data to move, shift it to the right.</span>
<a name="l00092"></a>00092   <span class="keywordflow">if</span> (move_bytes &gt; 0) {
<a name="l00093"></a>00093     <span class="keyword">const</span> std::string&amp; data_to_move = data_.substr(move_offset, move_bytes);
<a name="l00094"></a>00094     data_.replace(move_offset + slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>, move_bytes, data_to_move);
<a name="l00095"></a>00095   }
<a name="l00096"></a>00096   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a3af3c066d7651294fb3aae104f404648">free_space_upper_bound</a> += slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a>;
<a name="l00097"></a>00097 
<a name="l00098"></a>00098   <span class="comment">// Mark slot as unused.</span>
<a name="l00099"></a>00099   slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a4ea5ad6e73525244bb368181f21fd018">used</a> = <span class="keyword">false</span>;
<a name="l00100"></a>00100   slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a81c14b0e942b0d59e6ac31667d958b79">item_offset</a> = 0;
<a name="l00101"></a>00101   slot-&gt;<a class="code" href="structbadgerdb_1_1_page_slot.html#a675118fb03cd9e1b35970a1f10bda9d8">item_length</a> = 0;
<a name="l00102"></a>00102   ++header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a>;
<a name="l00103"></a>00103 
<a name="l00104"></a>00104   <span class="keywordflow">if</span> (allow_slot_compaction &amp;&amp; record_id.<a class="code" href="structbadgerdb_1_1_record_id.html#a19ec7b749099499446fdd24d48cec9ef">slot_number</a> == header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a>) {
<a name="l00105"></a>00105     <span class="comment">// Last slot in the list, so we need to free any unused slots that are at</span>
<a name="l00106"></a>00106     <span class="comment">// the end of the slot list.</span>
<a name="l00107"></a>00107     <span class="keywordtype">int</span> num_slots_to_delete = 1;
<a name="l00108"></a>00108     <span class="keywordflow">for</span> (<a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> i = 1; i &lt; header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a>; ++i) {
<a name="l00109"></a>00109       <span class="comment">// Traverse list backwards, looking for unused slots.</span>
<a name="l00110"></a>00110       <span class="keyword">const</span> PageSlot* other_slot = getSlot(header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a> - i);
<a name="l00111"></a>00111       <span class="keywordflow">if</span> (!other_slot-&gt;used) {
<a name="l00112"></a>00112         ++num_slots_to_delete;
<a name="l00113"></a>00113       } <span class="keywordflow">else</span> {
<a name="l00114"></a>00114         <span class="comment">// Stop at the first used slot we find, since we can&#39;t move used slots</span>
<a name="l00115"></a>00115         <span class="comment">// without affecting record IDs.</span>
<a name="l00116"></a>00116         <span class="keywordflow">break</span>;
<a name="l00117"></a>00117       }
<a name="l00118"></a>00118     }
<a name="l00119"></a>00119     header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a> -= num_slots_to_delete;
<a name="l00120"></a>00120     header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a> -= num_slots_to_delete;
<a name="l00121"></a>00121     header_.<a class="code" href="structbadgerdb_1_1_page_header.html#ac71c5a9b3b8644da0ea827f5bd4c5742">free_space_lower_bound</a> -= <span class="keyword">sizeof</span>(PageSlot) * num_slots_to_delete;
<a name="l00122"></a>00122   }
<a name="l00123"></a>00123 }
<a name="l00124"></a>00124 
<a name="l00125"></a><a class="code" href="classbadgerdb_1_1_page.html#a35bf91df5caa67a606681519740848b0">00125</a> <span class="keywordtype">bool</span> <a class="code" href="classbadgerdb_1_1_page.html#a35bf91df5caa67a606681519740848b0">Page::hasSpaceForRecord</a>(<span class="keyword">const</span> std::string&amp; record_data)<span class="keyword"> const </span>{
<a name="l00126"></a>00126   std::size_t record_size = record_data.length();
<a name="l00127"></a>00127   <span class="keywordflow">if</span> (header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a> == 0) {
<a name="l00128"></a>00128     record_size += <span class="keyword">sizeof</span>(<a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>);
<a name="l00129"></a>00129   }
<a name="l00130"></a>00130   <span class="keywordflow">return</span> record_size &lt;= <a class="code" href="classbadgerdb_1_1_page.html#af3d324b5aae656030465e213e86e2070">getFreeSpace</a>();
<a name="l00131"></a>00131 }
<a name="l00132"></a>00132 
<a name="l00133"></a>00133 <a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>* Page::getSlot(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> slot_number) {
<a name="l00134"></a>00134   <span class="keywordflow">return</span> <span class="keyword">reinterpret_cast&lt;</span><a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>*<span class="keyword">&gt;</span>(
<a name="l00135"></a>00135       &amp;data_[(slot_number - 1) * <span class="keyword">sizeof</span>(<a class="code" href="structbadgerdb_1_1_page_slot.html" title="Slot metadata that tracks where a record is in the data space.">PageSlot</a>)]);
<a name="l00136"></a>00136 }
<a name="l00137"></a>00137 
<a name="l00138"></a>00138 <span class="keyword">const</span> PageSlot&amp; Page::getSlot(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> slot_number)<span class="keyword"> const </span>{
<a name="l00139"></a>00139   <span class="keywordflow">return</span> *<span class="keyword">reinterpret_cast&lt;</span><span class="keyword">const </span>PageSlot*<span class="keyword">&gt;</span>(
<a name="l00140"></a>00140       &amp;data_[(slot_number - 1) * <span class="keyword">sizeof</span>(PageSlot)]);
<a name="l00141"></a>00141 }
<a name="l00142"></a>00142 
<a name="l00143"></a>00143 <a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> Page::getAvailableSlot() {
<a name="l00144"></a>00144   <a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> slot_number = <a class="code" href="classbadgerdb_1_1_page.html#a4013fab43df72bc52931241575736353">INVALID_SLOT</a>;
<a name="l00145"></a>00145   <span class="keywordflow">if</span> (header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a> &gt; 0) {
<a name="l00146"></a>00146     <span class="comment">// Have an allocated but unused slot that we can reuse.</span>
<a name="l00147"></a>00147     <span class="keywordflow">for</span> (<a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> i = 1; i &lt;= header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a>; ++i) {
<a name="l00148"></a>00148       <span class="keyword">const</span> PageSlot* slot = getSlot(i);
<a name="l00149"></a>00149       <span class="keywordflow">if</span> (!slot-&gt;used) {
<a name="l00150"></a>00150         <span class="comment">// We don&#39;t decrement the number of free slots until someone actually</span>
<a name="l00151"></a>00151         <span class="comment">// puts data in the slot.</span>
<a name="l00152"></a>00152         slot_number = i;
<a name="l00153"></a>00153         <span class="keywordflow">break</span>;
<a name="l00154"></a>00154       }
<a name="l00155"></a>00155     }
<a name="l00156"></a>00156   } <span class="keywordflow">else</span> {
<a name="l00157"></a>00157     <span class="comment">// Have to allocate a new slot.</span>
<a name="l00158"></a>00158     slot_number = header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a> + 1;
<a name="l00159"></a>00159     ++header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a>;
<a name="l00160"></a>00160     ++header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a>;
<a name="l00161"></a>00161     header_.<a class="code" href="structbadgerdb_1_1_page_header.html#ac71c5a9b3b8644da0ea827f5bd4c5742">free_space_lower_bound</a> = <span class="keyword">sizeof</span>(PageSlot) * header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a>;
<a name="l00162"></a>00162   }
<a name="l00163"></a>00163   assert(slot_number != <a class="code" href="classbadgerdb_1_1_page.html#a4013fab43df72bc52931241575736353">INVALID_SLOT</a>);
<a name="l00164"></a>00164   <span class="keywordflow">return</span> <span class="keyword">static_cast&lt;</span><a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a><span class="keyword">&gt;</span>(slot_number);
<a name="l00165"></a>00165 }
<a name="l00166"></a>00166 
<a name="l00167"></a>00167 <span class="keywordtype">void</span> Page::insertRecordInSlot(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#afe9f2f985e7c67e04f76a16f7c4500c8" title="Identifier for a slot in a page.">SlotId</a> slot_number,
<a name="l00168"></a>00168                               <span class="keyword">const</span> std::string&amp; record_data) {
<a name="l00169"></a>00169   <span class="keywordflow">if</span> (slot_number &gt; header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a6c233369c611cfb617ba676e11a9d87d">num_slots</a> ||
<a name="l00170"></a>00170       slot_number == <a class="code" href="classbadgerdb_1_1_page.html#a4013fab43df72bc52931241575736353">INVALID_SLOT</a>) {
<a name="l00171"></a>00171     <span class="keywordflow">throw</span> InvalidSlotException(<a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>(), slot_number);
<a name="l00172"></a>00172   }
<a name="l00173"></a>00173   PageSlot* slot = getSlot(slot_number);
<a name="l00174"></a>00174   <span class="keywordflow">if</span> (slot-&gt;used) {
<a name="l00175"></a>00175     <span class="keywordflow">throw</span> SlotInUseException(<a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>(), slot_number);
<a name="l00176"></a>00176   }
<a name="l00177"></a>00177   <span class="keyword">const</span> <span class="keywordtype">int</span> record_length = record_data.length();
<a name="l00178"></a>00178   slot-&gt;used = <span class="keyword">true</span>;
<a name="l00179"></a>00179   slot-&gt;item_length = record_length;
<a name="l00180"></a>00180   slot-&gt;item_offset = header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a3af3c066d7651294fb3aae104f404648">free_space_upper_bound</a> - record_length;
<a name="l00181"></a>00181   header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a3af3c066d7651294fb3aae104f404648">free_space_upper_bound</a> = slot-&gt;item_offset;
<a name="l00182"></a>00182   --header_.<a class="code" href="structbadgerdb_1_1_page_header.html#a7afd8d3c6f22b25e24958ebef773175e">num_free_slots</a>;
<a name="l00183"></a>00183   data_.replace(slot-&gt;item_offset, slot-&gt;item_length, record_data);
<a name="l00184"></a>00184 }
<a name="l00185"></a>00185 
<a name="l00186"></a>00186 <span class="keywordtype">void</span> Page::validateRecordId(<span class="keyword">const</span> RecordId&amp; record_id)<span class="keyword"> const </span>{
<a name="l00187"></a>00187   <span class="keywordflow">if</span> (record_id.page_number != <a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>()) {
<a name="l00188"></a>00188     <span class="keywordflow">throw</span> InvalidRecordException(record_id, <a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>());
<a name="l00189"></a>00189   }
<a name="l00190"></a>00190   <span class="keyword">const</span> PageSlot&amp; slot = getSlot(record_id.slot_number);
<a name="l00191"></a>00191   <span class="keywordflow">if</span> (!slot.used) {
<a name="l00192"></a>00192     <span class="keywordflow">throw</span> InvalidRecordException(record_id, <a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>());
<a name="l00193"></a>00193   }
<a name="l00194"></a>00194 }
<a name="l00195"></a>00195 
<a name="l00196"></a><a class="code" href="classbadgerdb_1_1_page.html#aa0fdb281074cd60cb1f1d6ea6d620772">00196</a> <a class="code" href="classbadgerdb_1_1_page_iterator.html" title="Iterator for iterating over the records in a page.">PageIterator</a> <a class="code" href="classbadgerdb_1_1_page.html#aa0fdb281074cd60cb1f1d6ea6d620772">Page::begin</a>() {
<a name="l00197"></a>00197   <span class="keywordflow">return</span> <a class="code" href="classbadgerdb_1_1_page_iterator.html" title="Iterator for iterating over the records in a page.">PageIterator</a>(<span class="keyword">this</span>);
<a name="l00198"></a>00198 }
<a name="l00199"></a>00199 
<a name="l00200"></a><a class="code" href="classbadgerdb_1_1_page.html#a8e78cba69bef682a5427932485da4608">00200</a> <a class="code" href="classbadgerdb_1_1_page_iterator.html" title="Iterator for iterating over the records in a page.">PageIterator</a> <a class="code" href="classbadgerdb_1_1_page.html#a8e78cba69bef682a5427932485da4608">Page::end</a>() {
<a name="l00201"></a>00201   <span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_record_id.html" title="Identifier for a record in a page.">RecordId</a>&amp; end_record_id = {<a class="code" href="classbadgerdb_1_1_page.html#af55a0b44876bff4d513bdbed8bc8c5a3">page_number</a>(), <a class="code" href="classbadgerdb_1_1_page.html#a4013fab43df72bc52931241575736353">Page::INVALID_SLOT</a>};
<a name="l00202"></a>00202   <span class="keywordflow">return</span> <a class="code" href="classbadgerdb_1_1_page_iterator.html" title="Iterator for iterating over the records in a page.">PageIterator</a>(<span class="keyword">this</span>, end_record_id);
<a name="l00203"></a>00203 }
<a name="l00204"></a>00204 
<a name="l00205"></a>00205 }
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Friends</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Wed Jan 30 2013 14:17:56 for BadgerDB by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
